package com._CPA_.service.impl;


import com._CPA_.dao.UserMapper;
import com._CPA_.pojo.UserInfo;
import com._CPA_.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import org.springframework.web.bind.annotation.RequestBody;
@Service

public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 生成用户唯一的id
     * @return
     */
    private  String creatUserCodeU() {
        UserInfo u=new UserInfo();

        boolean b = true;
        String usercode = "";
        while (b) {
            usercode = String.valueOf((int) ((Math.random() * 9 + 1) * 100000*100));
            u.setUserId(usercode);
            int have = userMapper.selectCount(u);
            if (have == 0) {
                b = false;
            }
        }
        return usercode;
    }





    /**
     * 用户名登录处理
     * @param user
     * @return
     */
    @Override
    public UserInfo loginByName(UserInfo user) {
        Example em = new Example(UserInfo.class);//可以封装任何条件,需要给一个泛型
//       将条件封装到example的内部类
        Example.Criteria criteria = em.createCriteria();
//       查询条件
        criteria.andEqualTo("userName",user.getUserName());
        criteria.andEqualTo("password",user.getPassword());


        return userMapper.selectOneByExample(em);//根据条件返回对应的用户对象的所有信息
    }
    /**
     * 手机号登录处理
     */
    @Override
    public  UserInfo loginByPhone(UserInfo user){
        Example em =new Example(UserInfo.class);
        Example.Criteria criteria= em.createCriteria();;
        criteria.andEqualTo("phone",user.getPhone());
        criteria.andEqualTo("password",user.getPassword());
        return  userMapper.selectOneByExample(em);
    }
    /**
     * 用户注册处理
     * @param user
     * @return
     */
    @Override
    public void register( UserInfo user) {
        /**
         * 初始化用户信息
         */
        user.setUserId(creatUserCodeU());
       if(user.getGender()==null){
           user.setGender("男");
       }
        if(user.getNickName()==null){
            user.setNickName(user.getUserName());
        }
        if (user.getUserIcon()==null){
            user.setUserIcon("http://43.139.94.130:6900/cpa-images/3A8B3D23203D41A68882E4E614A1DD48.png");

        }

        user.setCreateTime(LocalDateTime.now());
        user.setEditTime(LocalDateTime.now());
        user.setIfAdmin((short) 0);
        user.setIfVip((short)0);
        userMapper.insert(user);


    }



    /**
     *  确定用户的电话号码唯一
     * @return
     */
    @Override
    public boolean isOnlyPhone(String userPhone){
        if (userPhone.equals(userMapper.selectPhone(userPhone))){
            return false;
        }
        return true;
    }



    /**
     *  确定用户的电话号码唯一
     * @return
     */
    @Override
    public boolean isOnlyName(String userName){
        if (userName.equals(userMapper.selectUserName(userName))){
            return false;
        }
        return true;
    }

    /**
     * 用户注销的方法
     * @param name
     */
    @Override
    public void isOnlyDelete(String name) {
        userMapper.isDelete(name);
    }

    /**
     * 修改信息的方法
     * @param user
     */
    @Override
    public void isOnlyUpdate(UserInfo user) {
        user.setEditTime(LocalDateTime.now());
        userMapper.isUpdate(user);
    }

    /**
     * 判断密码是否重复的方法
     * @param password
     * @param user_id
     * @return
     */
    @Override
    public boolean isOnlyGetPassWord(String password,String user_id) {
        if (password.equals(userMapper.selectPassWord(user_id))){
            return false;
        }
        else {
            return true;
        }
    }

    /**
     * 修改密码的方法
     * @param u
     */
    @Override
    public void isOnlyPutPassWord(UserInfo u) {
        userMapper.isPassWordUpdate(u);
    }

    @Override
    public UserInfo selectUserAll(String id) {
        return userMapper.selectUserAll(id);
    }

}
